package cn.iocoder.yudao.module.fengdong.smartelderly.controller.app.useroldman;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.useroldman.vo.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.convert.useroldman.UserOldmanConvert;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.dataobject.useroldman.UserOldmanDO;
import cn.iocoder.yudao.module.fengdong.smartelderly.service.useroldman.UserOldmanService;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.dal.mysql.user.MemberUserMapper;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "用户 APP - 用户关联老人")
@RestController
@RequestMapping("/fengdong.smartelderly/user-oldman")
@Validated
public class AppUserOldmanController {

    @Resource
    private UserOldmanService userOldmanService;
    @Resource
    private MemberUserMapper memberUserMapper;

    @PostMapping("/create")
    @Operation(summary = "创建用户关联老人")
    public CommonResult<Long> createUserOldman(@Valid @RequestBody UserOldmanCreateReqVO createReqVO) {
        return success(userOldmanService.createUserOldman(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户关联老人")
    public CommonResult<Boolean> updateUserOldman(@Valid @RequestBody UserOldmanUpdateReqVO updateReqVO) {
        userOldmanService.updateUserOldman(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户关联老人")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteUserOldman(@RequestParam("id") Long id) {
        userOldmanService.deleteUserOldman(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户关联老人")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<UserOldmanRespVO> getUserOldman(@RequestParam("id") Long id) {
        UserOldmanDO userOldman = userOldmanService.getUserOldman(id);
        return success(UserOldmanConvert.INSTANCE.convert(userOldman));
    }

    @GetMapping("/list")
    @Operation(summary = "获得用户关联老人列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    public CommonResult<List<UserOldmanRespVO>> getUserOldmanList(@RequestParam("ids") Collection<Long> ids) {
        List<UserOldmanDO> list = userOldmanService.getUserOldmanList(ids);
        return success(UserOldmanConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户关联老人分页")
    public CommonResult<PageResult<UserOldmanRespVO>> getUserOldmanPage(@Valid UserOldmanPageReqVO pageVO) {
        PageResult<UserOldmanDO> pageResult = userOldmanService.getUserOldmanPage(pageVO);
        return success(UserOldmanConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/getother")
    @Operation(summary = "获得其他联络人")
    public CommonResult<List<MemberUserDO>> getother(@Valid  UserOldmanReqVO pageVO) {
        List<UserOldmanDO> userOldman = userOldmanService.getother(pageVO);
        List<String> collect = userOldman.stream()
                .map(UserOldmanDO::getCreator)
                .collect(Collectors.toList());
        List<MemberUserDO> memberUserDOS = memberUserMapper.selectBatchIds(userOldman);
        return success(memberUserDOS);
    }
}
